package com.hna.eking.framework.handler;


import com.hna.eking.framework.exception.BadRequestParamException;
import com.hna.eking.framework.exception.HttpStatusErrorEnum;
import com.hna.eking.framework.exception.InvalidClientException;
import com.hna.eking.service.framework.common.ErrorCode;
import com.hna.eking.service.framework.common.ServiceResultGenerator;
import com.hna.eking.service.framework.result.ServiceResult;
import feign.FeignException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@ControllerAdvice
@ResponseBody
public class ControllerExceptionHandler {
    private static final Logger logger = LoggerFactory.getLogger(ControllerExceptionHandler.class);

    @ExceptionHandler({Exception.class})
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public ServiceResult<ErrorInfo> handleException(HttpServletRequest req, HttpServletResponse res, Exception e) {
        logger.error(e.getMessage(), e);
        ErrorInfo errorInfo = new ErrorInfo(e.getClass().getCanonicalName(), e.getMessage());
        return ServiceResultGenerator.genResult(ErrorCode.ERROR_GLOBAL_SERVER_ERROR, errorInfo, null);
    }


    @ExceptionHandler({FeignException.class})
    public ServiceResult<ErrorInfo> handleFeginException(HttpServletRequest req, HttpServletResponse res, FeignException e) {
        logger.error(e.getMessage(), e);
        res.setStatus(HttpStatusErrorEnum.REMOTE_SERVICE_ERROR_STATUS.getCode());
        ErrorInfo errorInfo = new ErrorInfo(e.getClass().getCanonicalName(), e.getMessage());
        return ServiceResultGenerator.genResult(ErrorCode.ERROR_GLOBAL_REMOTE_SERVICE_ERROR, errorInfo, null);
    }

    @ExceptionHandler({BadRequestParamException.class})
    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    public ServiceResult<Object> handleBadRequestParamException(HttpServletRequest req, HttpServletResponse res, BadRequestParamException e) {
        logger.error(e.getMessage(), e);
        return ServiceResultGenerator.genResult(ErrorCode.ERROR_GLOBAL_PARAM_ERROR, e.getErrorInfoList(), null);
    }

    @ExceptionHandler({InvalidClientException.class})
    @ResponseStatus(value = HttpStatus.UNAUTHORIZED)
    public ServiceResult handleInvalidClientException(HttpServletRequest req, HttpServletResponse res, InvalidClientException e) {
        logger.error(e.getMessage(), e);
        return e.getServiceResult();
    }


    public static class ErrorInfo {
        private String exception;
        private String message;

        public ErrorInfo(String exception, String message) {
            this.exception = exception;
            this.message = message;
        }

        public String getException() {
            return exception;
        }

        public void setException(String exception) {
            this.exception = exception;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }
    }
}
